package vip.liux.front.infrastructure.jdbc.chat;

import org.springframework.data.jdbc.repository.query.Query;
import vip.liux.front.domain.models.chat.ChatUser;
import vip.liux.front.domain.models.chat.IChatUserRepository;

import java.util.Collection;
import java.util.List;

public interface ChatUserJdbcRepository extends IChatUserRepository {

    @Override
    @Query("SELECT * FROM chat_users WHERE id in (:ids)")
    List<ChatUser> getList(Collection<Long> ids);

    @Override
    @Query("SELECT * FROM chat_users WHERE ifnull(:filter, '') = '' or user_name like concat('%', :filter, '%') or email like concat('%', :filter, '%') or name like concat('%', :filter, '%') or surname like concat('%', :filter, '%')")
    long count(String filter);

    @Override
    @Query("SELECT * FROM chat_users WHERE ifnull(:filter, '') = '' or user_name like concat('%', :filter, '%') or email like concat('%', :filter, '%') or name like concat('%', :filter, '%') or surname like concat('%', :filter, '%') ORDER BY :sorting LIMIT :maxResultCount OFFSET :skipCount")
    List<ChatUser> search(String sorting, String filter, int maxResultCount, int skipCount);

    @Query("SELECT * FROM chat_users order by rand() limit 1")
    ChatUser selectByRandom();

    @Query("SELECT * FROM chat_users where id != :id order by rand() limit 1")
    ChatUser selectByRandom(Long id);
}
